package com.net.service;
import java.util.regex.*;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.net.domain.*;

public class UserValidator implements Validator {

   @Override
    public boolean supports(Class clazz) {
        return User.class.isAssignableFrom(clazz);
    }
    public void validate(Object obj, Errors errors) {
    	User user = (User) obj;     

    	if ((user.getPrimaryEmail() != "") || (user.getPrimaryEmail().length()) != 0) {    		
    		Pattern p=Pattern.compile(".+@.+\\.[a-z]+");
        	Matcher m=p.matcher(user.getPrimaryEmail());
        	boolean b=m.matches();
        	if(b!=true)
        	{
        		 errors.rejectValue("emailid", "error.is.not.valid", "Email ID does not Valid ");    		
        	}
        }
    	
    	if ((user.getLandLineNo() != "") || (user.getLandLineNo().length()) != 0) {    		
    			Pattern pattern = Pattern.compile("\\d{1}-\\d{4}-\\d{6}");
    			Matcher matcher = pattern.matcher(user.getLandLineNo());
    			boolean con=matcher.matches();
    			if(con!=true)
    				{
    					errors.rejectValue("contact", "error.is.not.valid", "Enter Contact Number Like 0-9999-999999");
    				}
        }
    	if ((user.getDob() != "") || (user.getDob().length()) != 0) {    		
			Pattern pattern = Pattern.compile("\\d{2}/\\d{2}/\\d{4}");
			Matcher matcher = pattern.matcher(user.getDob());
			boolean DOB=matcher.matches();
			if(DOB!=true)
				{
					errors.rejectValue("dob", "error.is.not.valid", "Enter Date of birth Like 01/02/1986 ");
				}
    }
    	if (user.getFirstName() == null || user.getFirstName().length() == 0) {
            errors.rejectValue("name", "error.empty.field", "Please Enter Name");
        }        
        if (user.getDob() == null || user.getDob().length() == 0) {
            errors.rejectValue("dob", "error.empty.field", "Please Enter Date Of Birth");
        }        
        if (user.getLandLineNo() == null || user.getLandLineNo().length() == 0) {
            errors.rejectValue("contact", "error.empty.field", "Please Enter Contact Number");
        }        
        if (user.getPrimaryEmail() == null || user.getPrimaryEmail().length() == 0) {
            errors.rejectValue("emailid", "error.empty.field", "Please Enter Email ID");
        }
        
        
        if (user.getCommunicationAddress() == null || user.getCommunicationAddress().length() == 0) {
            errors.rejectValue("address", "error.empty.field", "Please Enter Address");
        }
    }
}



 
